package com.zhengyisky.entity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.StringJoiner;

public class Data {

    private Long id;
    private String content;
    private LocalDateTime createTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String toCsvRow() {
        StringJoiner sj = new StringJoiner(",");
        sj.add(escapeCsv(String.valueOf(id)))
                .add(escapeCsv(content))
                .add(escapeCsv(createTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)));
        return sj.toString();
    }

    private String escapeCsv(String value) {
        if (value.contains(",") || value.contains("\"") || value.contains("\n")) {
            return "\"" + value.replace("\"", "\"\"") + "\"";
        }
        return value;
    }
}
